package com.woniuxy.main.dynamic_lzq.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.main.dynamic_lzq.mapper.DynamicMapper;
import com.woniuxy.main.dynamic_lzq.pojo.Dynamic;
import com.woniuxy.main.dynamic_lzq.pojo.DynamicComment;
import com.woniuxy.main.dynamic_lzq.pojo.User;
import com.woniuxy.main.dynamic_lzq.service.DynamicService;
import com.woniuxy.main.dynamic_lzq.service.UserService_Lzq;
import com.woniuxy.main.login_yjj.pojo.User_Yjj;

import lombok.Data;

@Service
@Transactional
@Data
public class DynamicServiceImpl implements DynamicService{
	@Autowired
	private DynamicMapper dynamicMapper;
	@Autowired
	private UserService_Lzq userService;
		@Override
		public void addDynamic(String dynamic_detail, String fileName, HttpServletRequest request) {
			User_Yjj currentUser =  (User_Yjj)request.getSession().getAttribute("currentUser");
			int user_id = currentUser.getUser_id();
			Dynamic dynamic = new Dynamic();
			dynamic.setDynamic_detail(dynamic_detail);
			dynamic.setUser_id(user_id);
			dynamic.setImg1(fileName);
			dynamic.setDynamic_createtime(new Date());
			dynamicMapper.addDynamic(dynamic);
		}

		@Override
		public List<Dynamic> findDynamicByUid(int user_id) {
			return dynamicMapper.findDynamicByUid(user_id);
		}

		@Override
		public void addComment(DynamicComment comment) {
			comment.setComment_createtime(new Date());
			dynamicMapper.addComment(comment);
		}

		@Override
		public List<DynamicComment> findCommentByDynamicId(int dynamic_id) {
			
			return dynamicMapper.findCommentByDynamicId(dynamic_id);
		}

		@Override
		public List<User> findUsersByDynamicId(int dynamic_id) {
			List<Integer> uids =  dynamicMapper.findUidsByDynamicId(dynamic_id);
			List<User> users = new ArrayList<User>();
			for(int i=0;i<uids.size();i++){
				User user = userService.findUserByUid(uids.get(i));
				users.add(user);
			}
			return users;
		}

		@Override
		public void retransmission(Dynamic dynamic) {
			int user_id = dynamic.getUser_id();
			dynamic = dynamicMapper.findDynamicByDynamicId(dynamic.getDynamic_id());
			dynamic.setDynamic_createtime(new Date());
			dynamic.setUser_id(user_id);
			dynamic.setDynamic_praise(null);
			dynamicMapper.addDynamic(dynamic);
		}

		@Override
		public void praise(int user_id, int dynamic_id) {
			String name = userService.findUserByUid(user_id).getUser_name();
			Dynamic dynamic = dynamicMapper.findDynamicByDynamicId(dynamic_id);
			String oldPraiseNames = dynamic.getDynamic_praise();
			String newPraiseNames = null;
			//System.out.println("old:"+oldPraiseNames);
			if(oldPraiseNames==null||oldPraiseNames.equals("")){
				newPraiseNames = name;
			}else if(oldPraiseNames.contains(name)){
				newPraiseNames = oldPraiseNames.replace("、"+name, "");
				newPraiseNames = oldPraiseNames.replace(name, "");
			}else{
				newPraiseNames = oldPraiseNames+"、"+name;
			}
			//System.out.println("new"+newPraiseNames);
			dynamic.setDynamic_id(dynamic_id);
			dynamic.setDynamic_praise(newPraiseNames);
			dynamicMapper.updatePraise(dynamic);
			
		}
}
